//
//  JCClubApi.m
//  SquashSpark
//
//  Created by 郭吉成 on 2017/11/23.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "JCClubApi.h"
#import "JCClubModel.h"
#import "JCClubMessageModel.h"
#import "JCClubEventModel.h"
#import "NSString+JCMD5Encryption.h"
#import "YFLoginHelper.h"
#import "JCClubMemberModel.h"
#import "JCReportProModel.h"
#import "JCReportCommentModel.h"
#import "JCReportModel.h"

@implementation JCClubApi

+ (JCClubApi *)getClubHomeListPage:(NSInteger)page
                          complete:(void(^)(NSError *error, NSArray <JCClubModel *>*recommendClubs, NSArray <JCClubModel *>*clubList)) complete{
    NSDictionary *param = @{@"page":@(page).stringValue};
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubFind andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            NSDictionary *modeDic = [JCClubModel creatFindClubList:[result responseInfo]];
            complete(nil,modeDic[@"recommed"] ,modeDic[@"list"]);
        }else if(complete){
            complete(error, nil, nil);
        }
    }];
    return clubApi;
}

- (NSTimeInterval)requestTimeoutInterval {
    return 6;
}

//搜索俱乐部
+ (JCClubApi *)searchClubInfoWithName:(NSString *)name
                             complete:(void(^)(NSError *error, NSArray <JCClubModel *> *resultClubs)) complete{
    NSDictionary *param = @{@"name":name};
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubSearchClub andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            NSMutableArray *results = [NSMutableArray array];
            NSArray *array = [[result responseInfo][@"ClubList"] isEqual:[NSNull null]]?@[]:[result responseInfo][@"ClubList"];
            for (NSDictionary *dic in array) {
                JCClubModel *model = [JCClubModel creatModelWithData:dic];
                [results addObject:model];
            }
            complete(nil,results.mutableCopy);
        }else if(complete){
            complete(error, nil);
        }
    }];
    return clubApi;
}

+ (JCClubApi *)getClubInfoWithClubID:(NSString *)clubID
                            complete:(void (^)(NSError *, JCClubModel *))complete{
    NSDictionary *param = @{@"ID":clubID};
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubInfo andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            JCClubModel *model = [JCClubModel creatModelWithData:[result responseInfo]];
            complete(nil, model);
        }else if(complete){
            complete(error, nil);
        }
    }];
    return clubApi;
}

//俱乐部留言板
+ (JCClubApi *)getClubMessageListWithClubID:(NSString *)clubID
                                       page:(NSInteger)page
                                   complete:(void(^)(NSError *error, NSArray <JCClubMessageModel *>*messageList)) complete{
    NSDictionary *param = @{@"ID":clubID,
                            @"page":@(page).stringValue
                            };
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubMessageList andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil, [JCClubMessageModel creatFindClubList:[result responseInfo]]);
        }else if(complete){
            complete(error, nil);
        }
    }];
    return clubApi;
}

//发布俱乐部留言
+ (JCClubApi *)sendClubMessageWithClubID:(NSString *)clubID
                                 content:(NSString *)content
                                complete:(void(^)(NSError *error)) complete{
    NSDictionary *param = @{@"ID":clubID,
                            @"Content":content
                            };
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubSendMessage andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil);
        }else if(complete){
            complete(error);
        }
    }];
    return clubApi;
}

//创建俱乐部
+ (JCClubApi *)creatClubCode:(NSString *)clubCode
                        name:(NSString *)name
                    describe:(NSString *)describe
                      banner:(NSString *)banner
                  headerIcon:(NSString *)headerIcon
                     address:(NSString *)address
                   clubPhone:(NSString *)clubPhone
               personalPhone:(NSString *)personalPhone
                    complete:(void(^)(NSError *error, BOOL isSuccess)) complete{
    NSDictionary *param = @{@"clubCode":clubCode?clubCode:@"",
                            @"name":name?name:@"",
                            @"describe":describe?describe:@"",
                            @"banner":banner?banner:@"",
                            @"headIcon":headerIcon?headerIcon:@"",
                            @"address":address?address:@"",
                            @"clubPhone":clubPhone?clubPhone:@"",
                            @"personalPhone":personalPhone?personalPhone:@""
                            };
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubCreatClub andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            [YFLoginHelper getuserInfoItemCompleted:^(NSError *loginError) {
                if (loginError) {
                    complete(loginError, NO);
                }else{
                    complete(nil, YES);
                }
            }];
        }else if(complete){
            complete(error, NO);
        }
    }];
    return clubApi;
}

//获取俱乐部用户列表详情数据
+ (JCClubApi *)getClubMemberDetailWithClubID:(NSString *)clubID
                                    complete:(void(^)(NSError *error, JCClubModel *clubModel)) complete{
    NSDictionary *param = @{@"ID":clubID};
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubMemberDetail andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil,[JCClubModel creatModelWithData:[result responseInfo]]);
        }else if(complete){
            complete(error, nil);
        }
    }];
    return clubApi;
}

//获取俱乐部排行榜
+ (JCClubApi *)getClubMemberRankWithClubID:(NSString *)clubID
                        clubMemberRankTime:(ClubMemberRankTimeType)rankTimeType
                        clubMemberRankType:(ClubMemberRankType)rankType
                                  complete:(void(^)(NSError *error, NSArray <JCClubMemberModel *> *rankList)) complete{
    NSDictionary *param = @{@"ID":clubID,
                            @"type":@(rankTimeType).stringValue,
                            @"class":@(rankType).stringValue
                            };
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubMemberRank andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil,[JCClubMemberModel creatRankClubList:[result responseInfo]]);
        }else if(complete){
            complete(error, nil);
        }
    }];
    return clubApi;
}

//设置俱乐部
+ (JCClubApi *)setClubDate:(NSInteger)date
                     swing:(NSInteger)swing
                  duration:(NSInteger)duration
                    clubID:(NSString *)clubID
                  complete:(void(^)(NSError *error)) complete{
    NSDictionary *param = @{@"ID":clubID,
                            @"activeDays":@(date).stringValue,
                            @"swings":@(swing).stringValue,
                            @"duration":@(duration).stringValue
                            };
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubTargetSet andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil);
        }else if(complete){
            complete(error);
        }
    }];
    return clubApi;
}

//获取俱乐部事件
+ (JCClubApi *)getClubEventListWithClubID:(NSString *)clubID
                                     page:(NSInteger)page
                                 complete:(void(^)(NSError *error, NSArray <JCClubEventModel *>*eventList)) complete{
    NSDictionary *param = @{@"ID":clubID,
                            @"page":@(page).stringValue
                            };
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubEventList andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil,[JCClubEventModel creatFindClubList:[result responseInfo]]);
        }else if(complete){
            complete(error, nil);
        }
    }];
    return clubApi;
}

//创建俱乐部事件
+ (JCClubApi *)createClubEventWithClubID:(NSString *)clubID
                                   title:(NSString *)title
                                 content:(NSString *)content
                              bannerList:(NSArray *)bannerList
                                   place:(NSString *)place
                               startTime:(NSString *)startTime
                                 endTime:(NSString *)endTime
                                complete:(void(^)(NSError *error)) complete{
    NSString *bannerString;
    for (NSInteger i = 0; i < bannerList.count; i++) {
        if (i == 0) {
            bannerString = bannerList[i];
        }else{
            bannerString = [NSString stringWithFormat:@",%@",bannerList[i]];
        }
    }

    NSDictionary *param = @{@"clubID":clubID?clubID:@"",
                            @"title":title?title:@"",
                            @"bannerList":bannerString?bannerString:@"",
                            @"place":place?place:@"",
                            @"startTime":startTime?startTime:@"",
                            @"endTime":endTime?endTime:@"",
                            @"content":content?content:@""
                            };
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubCreatEvent andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil);
        }else if(complete){
            complete(error);
        }
    }];
    return clubApi;
}

//设置俱乐部密码
+ (JCClubApi *)setClubPasswordWithClubID:(NSString *)clubID
                                password:(NSString *)password
                                complete:(void(^)(NSError *error)) complete{
    NSDictionary *param = @{@"ID":clubID,
                            @"pwd":password};
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubSetPassword andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil);
        }else if(complete){
            complete(error);
        }
    }];
    return clubApi;
}

//申请加入俱乐部
+ (JCClubApi *)joinClubWithClubID:(NSString *)clubID
                         password:(NSString *)password
                         complete:(void(^)(NSError *error)) complete{
    NSDictionary *param = @{@"ID":clubID,
                            @"pwd":[password stringToMD5]};
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubJoinClub andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            //刷新登录
            [YFLoginHelper getuserInfoItemCompleted:^(NSError *loginError) {
                if (loginError) {
                    complete(loginError);
                }else{
                    complete(nil);
                }
            }];
        }else if(complete){
            complete(error);
        }
    }];
    return clubApi;
}

//获取周报告列表
//获取周报告列表
+ (JCClubApi *)getReportListForUser:(NSString *)userID
                           complete:(void(^)(NSError *error, NSArray <JCReportListModel *> *reportList)) complete{
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubMyReportList andParam:userID?@{@"userID":userID}:nil];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil, [JCReportListModel creatReportListWithSource:[result responseInfo]]);
        }else if(complete){
            complete(error, nil);
        }
    }];
    return clubApi;
}

//获取周报告详情
+ (JCClubApi *)getReportDetailForUserID:(NSString *)userID
                                 clubID:(NSString *)clubID
                                   year:(NSString *)year
                               yearWeek:(NSString *)yearWeek
                               complete:(void(^)(NSError *error, JCReportModel *reportModel)) complete{
    NSDictionary *param = @{@"userID":userID,
                            @"clubID":clubID,
                            @"year":year?year:@"",
                            @"yearWeek":yearWeek?yearWeek:@""
                            };
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubReportWeeklyDetail andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil, [JCReportModel creatReportDetailModelWith:[result responseInfo]]);
        }else if(complete){
            complete(error, nil);
        }
    }];
    return clubApi;
}

//获取周报告详情Pro
+ (JCClubApi *)getReportProForUserID:(NSString *)userID
                              clubID:(NSString *)clubID
                                year:(NSString *)year
                            yearWeek:(NSString *)yearWeek
                            complete:(void(^)(NSError *error, JCReportProModel *reportProModel)) complete{
    NSDictionary *param = @{@"userID":userID,
                            @"clubID":clubID,
                            @"year":year?year:@"",
                            @"yearWeek":yearWeek?yearWeek:@""
                            };
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubReportWeeklyDetailPro andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil, [JCReportProModel creatModelWith:[result responseInfo]]);
        }else if(complete){
            complete(error, nil);
        }
    }];
    return clubApi;
}

//管理员获取周报成员列表
+ (JCClubApi *)getReportMemberListForClubID:(NSString *)clubID
                       reportMemberListType:(ReportMemberListType)memberType
                                   complete:(void(^)(NSError *error, NSArray <JCClubMemberModel *> *memberList)) complete{
    NSDictionary *param = @{@"ID":clubID,
                            @"type":@(memberType).stringValue
                            };
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubReportMemberList andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil, [JCClubMemberModel creatRankClubList:[result responseInfo][@"MemberList"]]);
        }else if(complete){
            complete(error, nil);
        }
    }];
    return clubApi;
}

//教练评论周报
+ (JCClubApi *)commentReportForClubID:(NSString *)clubID
                               userID:(NSString *)userID
                                 year:(NSString *)year
                             yearWeek:(NSString *)yearWeek
                          coachReview:(NSString *)coachReview
                           suggestion:(NSString *)suggestion
                               course:(NSString *)course
                             complete:(void(^)(NSError *error)) complete{
    NSDictionary *param = @{@"userID":userID,
                            @"clubID":clubID,
                            @"year":year?year:@"",
                            @"yearWeek":yearWeek?yearWeek:@"",
                            @"coachReview":coachReview,
                            @"suggestion":suggestion,
                            @"course":course
                            };
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubReportCoachCommentUser andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil);
        }else if(complete){
            complete(error);
        }
    }];
    return clubApi;
}

//获取教练的点评周报
+ (JCClubApi *)getReportCommentForClubID:(NSString *)clubID
                                  userID:(NSString *)userID
                                    year:(NSString *)year
                                yearWeek:(NSString *)yearWeek
                                complete:(void(^)(NSError *error, JCReportCommentModel *commentModel)) complete{
    NSDictionary *param = @{@"userID":userID,
                            @"clubID":clubID,
                            @"year":year?year:@"",
                            @"yearWeek":yearWeek?yearWeek:@""
                            };
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubReportGetMyCoachComment andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil, [JCReportCommentModel creatReportCommentModelWith:[result responseInfo]]);
        }else if(complete){
            complete(error, nil);
        }
    }];
    return clubApi;
}

//退出俱乐部
+ (JCClubApi *)quitClubForClubID:(NSString *)clubID
                        complete:(void (^)(NSError *))complete{
    NSDictionary *param = @{@"clubID":clubID};
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubQuitClub andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            [[RLMRealm defaultRealm] transactionWithBlock:^{
                [[RLMRealm defaultRealm] deleteObjects:[User getUseInfo].joinClubs];
            }];
            [YFUserDefaults saveClubMeRefreshState:YES];
            complete(nil);
        }else if(complete){
            complete(error);
        }
    }];
    return clubApi;
}

//修改俱乐部资料
+ (JCClubApi *)editClubWithClubID:(NSString *)clubID
                             name:(NSString *)name
                        headerURL:(NSString *)headerURL
                         coverURL:(NSString *)coverURL
                          address:(NSString *)address
                         describe:(NSString *)describe
                         complete:(void(^)(NSError *error)) complete{
    NSDictionary *param = @{@"clubID":clubID,
                            @"name":name,
                            @"address":address,
                            @"describe":describe,
                            @"headIcon":headerURL?headerURL:@"",
                            @"banner":coverURL?coverURL:@""
                            };
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubEditClubInformation andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil);
        }else if(complete){
            complete(error);
        }
    }];
    return clubApi;
}

//删除俱乐部留言
+ (JCClubApi *)deleteMessageWithID:(NSString *)messageID
                          complete:(void(^)(NSError *error)) complete{
    NSDictionary *param = @{@"msgID":messageID};
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubDeleteClubMessage andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil);
        }else if(complete){
            complete(error);
        }
    }];
    return clubApi;
}

//删除俱乐部事件
+ (JCClubApi *)deleteEventWithID:(NSString *)eventID
                        complete:(void(^)(NSError *error)) complete{
    NSDictionary *param = @{@"eventID":eventID};
    JCClubApi *clubApi = [JCClubApi requestApiWithType:kClubDeleteClubEvent andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil);
        }else if(complete){
            complete(error);
        }
    }];
    return clubApi;
}

//获取七牛token
+ (JCClubApi *)getQiniuTokenType:(RequestApiType)type
                        complete:(void(^)(NSError *error, NSString *token, NSString *key)) complete{
    JCClubApi *clubApi = [JCClubApi requestApiWithType:type andParam:nil];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            NSDictionary *errDesc = [result responseInfo];
            complete(nil, errDesc[@"token"],errDesc[@"key"]);
        }else if(complete){
            complete(error, nil, nil);
        }
    }];
    return clubApi;
}

//举报 俱乐部 或者 留言
+ (JCClubApi *)spotterWithID:(NSString *) spotterID
               spotterIDType:(SpotterIDType)spotterTypeIDType
                 spotterType:(SpotterTopicType) spotterType
                    complete:(void(^)(NSError *error)) complete{
    NSString *keyID;
    NSDictionary *param;
    NSInteger apiType;
    
    if (spotterTypeIDType == SpotterIDTypePost) {
        param = @{@"postID":spotterID,
                  @"type":@(spotterType).stringValue};
        apiType = kMomentsSpotterTopic;
    }else{
        apiType = kClubSpotterClubOrMessage;
        if (spotterTypeIDType == SpotterIDTypeClub) {
            keyID = @"clubID";
        }else if (spotterTypeIDType == SpotterIDTypeMessage){
            keyID = @"msgID";
        }else if (spotterTypeIDType == SpotterIDTypeEvent){
            keyID = @"eventID";
        }else{
            keyID = @"error";
        }
        param = @{keyID:spotterID,
                  @"spotterType":@(spotterTypeIDType).stringValue,
                  @"type":@(spotterType).stringValue};
    }

    JCClubApi *clubApi = [JCClubApi requestApiWithType:apiType andParam:param];
    [[YFNetworkingService sharedInstance] sendRequest:clubApi callback:^(BOOL success, id result, NSError *error) {
        if (success && complete) {
            complete(nil);
        }else if(complete){
            complete(error);
        }
    }];
    return clubApi;
}

@end
